package com.sunday.volunteer.ui.mine;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.recyclerView.HorizontalDividerItemDecoration;
import com.sunday.member.base.BaseActivity;
import com.sunday.member.utils.SharePerferenceUtils;
import com.sunday.volunteer.BaseApp;
import com.sunday.volunteer.R;
import com.sunday.volunteer.adapter.GroupAdapter;
import com.sunday.volunteer.entity.Member;
import com.sunday.volunteer.entity.Product;
import com.sunday.volunteer.http.HttpClient;
import com.sunday.volunteer.utils.BaseUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by admin on 2017/1/12.
 */

public class GroupDetialActivity extends BaseActivity{

    @Bind(R.id.title_text)
    TextView title_text;
    @Bind(R.id.title_right_text)
    TextView title_right_text;
    @Bind(R.id.ratingStar)
    RatingBar ratingBar;
    @Bind(R.id.logo)
    ImageView logo;
    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.hours)
    TextView hours;
    @Bind(R.id.projects)
    TextView projects;
    @Bind(R.id.count)
    TextView count;
    @Bind(R.id.desc)
    TextView desc;
    @Bind(R.id.user_type)
    TextView user_type;

    private long groupId = -1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_detail);
        ButterKnife.bind(this);
        title_right_text.setText("申请加入");
        title_text.setText("团队详情");
        groupId = getIntent().getLongExtra(BaseUtil.ID , -1);

        getData();
    }

    public void getData(){
        Call<ResultDO<Member>> call = HttpClient.getHttpAdapter().getMemberDeatail(groupId);
        call.enqueue(new Callback<ResultDO<Member>>() {
            @Override
            public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                if(response.body() == null || isFinish ){
                    return;
                }
                if(response.body().getCode() == 0 && response.body().getResult()!= null){
                    Member member = response.body().getResult();
                    setData(member);
                }else {
                    ToastUtils.showToast(mContext , response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Member>> call, Throwable t) {

            }
        });
    }




    private void setData(Member item){
        ratingBar.setRating(4);

        BaseUtil.setImage(logo , item.getLogo() , 100,100);
        BaseUtil.setText(name , item.getName());
        BaseUtil.setText(hours , item.getHours());
        BaseUtil.setText(projects , item.getProjects());
        BaseUtil.setText(count , item.getJoinNumber());
        BaseUtil.setText(desc , item.getDesc());
        BaseUtil.setText(user_type , item.getUserTypeStr());

    }

    @OnClick(R.id.title_right_text)
    void join(){

        Call<ResultDO> call = HttpClient.getHttpAdapter().join(BaseApp.getInstance().getMemberId(), groupId);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext , "提交成功，请等待审核");

                }else {
                    ToastUtils.showToast(mContext , response.body().getMessage());
                }

            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {

            }
        });

    }


}
